package bb.lanxing.lib.devices.sprint.model.impl;

import android.content.Context;
import bb.lanxing.lib.devices.core.sync.DeviceFileStatus;
import bb.lanxing.lib.devices.core.utils.DeviceHelper;
import bb.lanxing.lib.devices.sprint.Command;
import bb.lanxing.lib.devices.sprint.Commands;
import bb.lanxing.lib.devices.sprint.SprintFile;
import bb.lanxing.lib.devices.sprint.SprintListener;
import bb.lanxing.lib.devices.sprint.XossDeviceController;
import bb.lanxing.lib.devices.sprint.entity.SprintMaps;
import bb.lanxing.lib.devices.sprint.utils.DeviceControllerHelper;
import bb.lanxing.lib.devices.sprint.utils.SprintFileHelper;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//import org.osmdroid.util.constants.UtilConstants;

public class SprintDeviceMapModel extends AbstractSprintMapModel implements SprintListener {
    private static final Pattern PATTERN_MAP = Pattern.compile("(\\d+)_V(\\d+).map");
    private XossDeviceController controller;
    private final Object lock;
    private long syncingId;

    public SprintDeviceMapModel(Context context, String str) {
        super(context);
        this.lock = new Object();
        XossDeviceController xossDeviceController = DeviceControllerHelper.getXossDeviceController(str);
        this.controller = xossDeviceController;
        xossDeviceController.registerXossDeviceListener(this);
    }

    @Override
    public List<SprintMaps> loadSprintMaps() {
        File file = new File(DeviceHelper.getWorkDir(9), SprintFileHelper.MAP_FILE_NAME);
        if (file.exists()) {
            onFileReceived(file.getAbsolutePath());
        } else {
            synchronized (this.lock) {
                this.controller.getFile(SprintFileHelper.MAP_FILE_NAME);
                try {
                    this.lock.wait(20000L/*UtilConstants.GPS_WAIT_TIME*/);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return getSprintMaps();
    }

    @Override
    public boolean syncWithDevice(SprintMaps sprintMaps) {
        if (sprintMaps == null) {
            return false;
        }
        File createFile = createFile(sprintMaps);
        if (createFile == null || !createFile.exists()) {
            notifyMapsStatus(sprintMaps.getId(), DeviceFileStatus.STATUS_SYNC_FAIL);
            return false;
        }
        notifyMapsStatus(sprintMaps.getId(), DeviceFileStatus.STATUS_SYNCING);
        this.syncingId = sprintMaps.getId();
        this.controller.sendFile(createFile.getAbsolutePath());
        return true;
    }

    @Override
    public void onProgressUpdate(Command command, float f) {
        if (command.getCmd() == 7) {
            long j = this.syncingId;
            if (j <= 0) {
                return;
            }
            notifyProgressUpdate(j, f);
        }
    }

    @Override
    public void onCmdStatus(Command command, int i, byte[] bArr) {
        byte cmd = command.getCmd();
        String parseFileName = Commands.parseFileName(command);
        Matcher matcher = PATTERN_MAP.matcher(parseFileName);
        long parseLong = matcher.matches() ? Long.parseLong(matcher.group(1)) : -1L;
        if (cmd == 5) {
            if (!SprintFileHelper.isMapListFile(parseFileName) || i == 0) {
                return;
            }
            setSprintMaps(null);
            synchronized (this.lock) {
                this.lock.notifyAll();
            }
        } else if (cmd == 7) {
            if (parseLong == -1) {
                return;
            }
            if (i == 0) {
                SprintFileHelper.clearMapsList(DeviceHelper.getWorkDir(9));
                notifyMapsStatus(parseLong, DeviceFileStatus.STATUS_SYNCED);
                return;
            }
            notifyMapsStatus(parseLong, DeviceFileStatus.STATUS_SYNC_FAIL);
        } else if (cmd != 13 || parseLong == -1) {
        } else {
            if (i == 0) {
                SprintFileHelper.clearMapsList(DeviceHelper.getWorkDir(9));
                super.deleteById(parseLong);
                return;
            }
            notifyMapsStatus(parseLong, DeviceFileStatus.STATUS_DELETE_FAIL);
        }
    }

    @Override
    public boolean isSynced(long j) {
        return getSprintMapById(j) != null;
    }

    @Override
    public void setSprintMaps(List<SprintMaps> list) {
        super.setSprintMaps(list);
        synchronized (this.lock) {
            this.lock.notifyAll();
        }
    }

    @Override
    public void deleteById(long j) {
        SprintMaps sprintMapById = getSprintMapById(j);
        if (sprintMapById == null) {
            notifyMapsStatus(j, DeviceFileStatus.STATUS_DELETE_FAIL);
            return;
        }
        this.controller.delete(j + "_V" + sprintMapById.getVersion() + ".map");
    }

    @Override
    public void onFileReceived(String str) {
        if (SprintFileHelper.isMapListFile(str)) {
            List<SprintFile> parseMapList = SprintFileHelper.parseMapList(str);
            if (parseMapList != null) {
                ArrayList arrayList = new ArrayList();
                for (SprintFile sprintFile : parseMapList) {
                    String fileName = sprintFile.getFileName();
                    Matcher matcher = PATTERN_MAP.matcher(fileName);
                    if (matcher.matches()) {
                        arrayList.add(new SprintMaps(fileName, fileName, Long.parseLong(matcher.group(1)), sprintFile.getSize(), Integer.parseInt(matcher.group(2))));
                    }
                }
                setSprintMaps(arrayList);
            } else {
                setSprintMaps(null);
            }
            synchronized (this.lock) {
                this.lock.notifyAll();
            }
        }
    }

    @Override
    public void release() {
        super.release();
        this.controller.unregisterXossDeviceListener(this);
        this.controller = null;
        synchronized (this.lock) {
            this.lock.notifyAll();
        }
    }
}
